package cn.eweb.ttkt;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.PluginState;
import cn.eweb.util.*;

public class HomeActivity extends Activity {

	WebView webView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webview);
        webView = (WebView)this.findViewById(R.id.wv);
        webView.getSettings().setPluginState(PluginState.ON);
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webView.getSettings().setJavaScriptEnabled(true);
     // 创建WebViewClient对象 
        WebViewClient wvc = new WebViewClient(){
        	@Override 
        	public boolean shouldOverrideUrlLoading(WebView view, String url) { 
        		// 使用自己的WebView组件来响应Url加载事件，而不是使用默认浏览器加载页面 
        		if(Tools.isNetWorkAvailable(getApplicationContext())){
					view.loadUrl(url);
				}else{
					view.loadUrl(getString(R.string.video_url));
				}
        		// 记得消耗掉这个事件。给不知道的朋友再解释一下，Android中返回True的意思就是到此为止吧,事件就会不会冒泡传递了，我们称之为消耗掉 
        		return true; 
        	} 
        	
        	private ProgressDialog pd;
        	
        	

        	@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
        		pd = new ProgressDialog(HomeActivity.this);
                pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                pd.setMessage("Loading");
                pd.show();
				super.onPageStarted(view, url, favicon);
			}

            @Override
            public void onPageFinished(WebView view, String url) {
                this.dismissDialog();
                super.onPageFinished(view, url);
            }

            private void dismissDialog() {
                if (pd != null) {
                    pd.dismiss();
                    pd = null;
                }
            }
        };
		webView.setWebViewClient(wvc);
		webView.setWebChromeClient(new WebChromeClient());
        if(Tools.isNetWorkAvailable(getApplicationContext())){
        	webView.loadUrl(getString(R.string.home_url)); 
        }else{
        	webView.loadUrl(getString(R.string.error_url));
        }
	}
	
	@Override
	protected void onPause() {
		webView.onPause();
		super.onPause();
	}
	
	@Override
	protected void onResume() {
		webView.onResume();
		if(webView.getOriginalUrl()!=null){
			if(Tools.isNetWorkAvailable(getApplicationContext())&&webView.getOriginalUrl().equals(getString(R.string.error_url))){
				webView.loadUrl(getString(R.string.home_url)); 
			}
		}
		super.onResume();
//		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
			if(webView.canGoBack()){
				webView.goBack(); //goBack()表示返回WebView的上一页面 
	            return true; 
			}else{
				AlertDialog.Builder ad = new AlertDialog.Builder(this);
	        	ad.setIcon(android.R.drawable.ic_dialog_info);
	    		ad.setTitle(getString(R.string.quit)); // 设置标题 
	            ad.setMessage(getString(R.string.quit_confirm));
	            ad.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {

	    			public void onClick(DialogInterface dialog, int which) {
	    				dialog.cancel();
	    				HomeActivity.this.finish();
	    			} 
	            });  
	            ad.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {

	    			public void onClick(DialogInterface dialog, int which) {
	    				dialog.cancel();
	    			}
	                   
	            });  
	            AlertDialog alert = ad.create();
	            alert.show();
			}
        }
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		/*
         * 
         * add()方法的四个参数，依次是：
         * 1、组别，如果不分组的话就写Menu.NONE,
         * 2、Id，这个很重要，Android根据这个Id来确定不同的菜单
         * 3、顺序，那个菜单现在在前面由这个参数的大小决定
         * 4、文本，菜单的显示文本
         */
        menu.add(Menu.NONE, Menu.FIRST + 1, 4, getString(R.string.quit)).setIcon(
        		android.R.drawable.ic_menu_revert);
        menu.add(Menu.NONE, Menu.FIRST + 2, 1, getString(R.string.back_to_home)).setIcon(
        		android.R.drawable.ic_menu_myplaces);
        menu.add(Menu.NONE, Menu.FIRST + 3, 2, getString(R.string.reload)).setIcon(
        		android.R.drawable.ic_menu_rotate);
        menu.add(Menu.NONE, Menu.FIRST + 4, 3, getString(R.string.about)).setIcon(
        		android.R.drawable.ic_menu_info_details);
        return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
        case Menu.FIRST + 1:
        	HomeActivity.this.finish();
            break;
        case Menu.FIRST + 2:
        	webView.loadUrl(getString(R.string.home_url)); 
            break;
        case Menu.FIRST + 3:
        	webView.reload();
            break;
        case Menu.FIRST + 4:
        	Intent intent = new Intent(Intent.ACTION_MAIN);  
        	Bundle bundle = new Bundle();  
        	bundle.putString("mode", "external");  
        	intent.putExtra("bd", bundle);
            intent.setClass(HomeActivity.this, AboutActivity.class);  
            HomeActivity.this.startActivity(intent);
            break;
		}
		return false;
	}

}
